/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                    *
*        ***************************************************         *
*        *©2021-2031 LINUO Corporation All rights reserved *         *
*        ***************************************************         *
*                                                                    *
* FileName    : isp.c                                                *
*                                                                    *
* Author      : linus                                                *
*                                                                    *
* Email       : luoyaojun@sina.com                                   *
*                                                                    *
* Date        : 2024-3-27                                            *
*                                                                    *
* Description :                                                      *
*                                                                    *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#include "h_isp.h"
#include "rk_debug.h"
#include "rk_mpi_sys.h"
#include "isp.h"


static int rockchip_isp_init_async(struct ht_isp *pisp, struct ht_isp_config *pisp_config)
{
	struct rockchip_isp *plt_isp = container_of(pisp, struct rockchip_isp, m_isp);
	int camera_count = 0;
	rk_aiq_static_info_t aiq_static_info;
	rk_aiq_sys_ctx_t *aiq_ctx = NULL;
	int ret = 0;
	
	printf("LINUS rockchip_isp_init_async ...\n");

	for (camera_count=0; camera_count<pisp_config->rconfig.camera_count; camera_count++)
	{
		memset(&aiq_static_info, 0, sizeof(rk_aiq_static_info_t));

		ret = rk_aiq_uapi2_sysctl_enumStaticMetas(camera_count, &aiq_static_info);

		aiq_ctx = rk_aiq_uapi2_sysctl_init(aiq_static_info.sensor_info.sensor_name, plt_isp->iqfiles_path, NULL, NULL);

		if (!aiq_ctx)
			break;

		plt_isp->camera_ctx[camera_count] = aiq_ctx;
		
		ret |= rk_aiq_uapi2_sysctl_preInit_scene(aiq_static_info.sensor_info.sensor_name, "normal", "day");
		
		//	 sensor多分辨率支持
		ret |= rk_aiq_uapi2_sysctl_prepare(aiq_ctx, pisp_config->rconfig.async_width, pisp_config->rconfig.async_height, RK_AIQ_WORKING_MODE_NORMAL);

		ret |= rk_aiq_uapi2_sysctl_start(aiq_ctx);
	}

	return ret;
}

static int rockchip_isp_init_sync(struct ht_isp *pisp, struct ht_isp_config *pisp_config)
{
	struct rockchip_isp *plt_isp = container_of(pisp, struct rockchip_isp, m_isp);
	int camera_count = 0;
	rk_aiq_static_info_t aiq_static_info;
	rk_aiq_camgroup_instance_cfg_t camgroup_cfg;
	rk_aiq_camgroup_ctx_t *aiq_ctx = NULL;
	char sensor_name_array[MAX_AIQ_CTX][128];
	int ret = 0;
	Uapi_ExpSwAttrV2_t expSwAttr;
	
	printf("LINUS rockchip_isp_init_sync ...\n");

	memset(&camgroup_cfg, 0, sizeof(rk_aiq_camgroup_instance_cfg_t));
	
	camgroup_cfg.sns_num = pisp_config->rconfig.camera_count;
	
	for (camera_count=0; camera_count<camgroup_cfg.sns_num; camera_count++)
	{
		memset(&aiq_static_info, 0, sizeof(rk_aiq_static_info_t));
		memset(sensor_name_array[camera_count], 0, sizeof(sensor_name_array[camera_count]));
		
		ret = rk_aiq_uapi2_sysctl_enumStaticMetas(camera_count, &aiq_static_info);
		
		memcpy(sensor_name_array[camera_count], aiq_static_info.sensor_info.sensor_name, strlen(aiq_static_info.sensor_info.sensor_name) + 1);
		camgroup_cfg.sns_ent_nm_array[camera_count] = sensor_name_array[camera_count];

		ret |= rk_aiq_uapi2_sysctl_preInit_scene(aiq_static_info.sensor_info.sensor_name, "normal", "day");

	}
	
	camgroup_cfg.config_file_dir = plt_isp->iqfiles_path;

	aiq_ctx = rk_aiq_uapi2_camgroup_create(&camgroup_cfg);

	if (!aiq_ctx)
		return -1;
	
	plt_isp->camera_ctx[0] = aiq_ctx;

	ret |= rk_aiq_uapi2_camgroup_prepare(aiq_ctx, RK_AIQ_WORKING_MODE_NORMAL);

	ret |= rk_aiq_uapi2_camgroup_start(aiq_ctx);

	ret |= rk_aiq_user_api2_ae_getExpSwAttr(aiq_ctx, &expSwAttr);
		
	expSwAttr.AecOpType = RK_AIQ_OP_MODE_AUTO;
	expSwAttr.stAdvanced.SetAeRangeEn = true;
	expSwAttr.stAuto.stFrmRate.isFpsFix = true;
	expSwAttr.stAuto.stFrmRate.FpsValue = 30;
	ret |= rk_aiq_user_api2_ae_setExpSwAttr(aiq_ctx, expSwAttr);
	
	return ret;
}


int rockchip_isp_init(struct ht_isp *pisp, struct ht_isp_config *pisp_config)
{
	int ret = -1;
	struct rockchip_isp *plt_isp = container_of(pisp, struct rockchip_isp, m_isp);

	if (pisp_config->rconfig.camera_count<=0 || pisp_config->rconfig.camera_count > MAX_AIQ_CTX)
		return ret;

	plt_isp->iqfiles_path = pisp_config->rconfig.iqfiles_path?:DEFAULT_IQ_FILES_PATH;
	
	ret = ht_isp_check_iqfiles(plt_isp->iqfiles_path);
	if (ret)
		plt_isp->iqfiles_path = DEFAULT_IQ_FILES_PATH;

	if (pisp_config->rconfig.mode == HT_ISP_SYNC)
		ret = rockchip_isp_init_sync(pisp, pisp_config);
	else
		ret = rockchip_isp_init_async(pisp, pisp_config);

	return ret;
}


static struct ht_isp_ops g_isp_ops = {
	//	check avalid camera
	
	.ht_isp_init = rockchip_isp_init,

	//	锐度

	//	亮度、色度、对比度、饱和度
	
	//	白平衡

	//	透雾

	//	降噪
	
};

static HT_S32 __init rockchip_isp_register(void)
{
    HT_S32 ret = -1;
    struct rockchip_isp *plt_isp = (struct rockchip_isp *)calloc(1, sizeof(struct rockchip_isp));
    struct ht_isp *pisp = NULL;

    if (!plt_isp)
        return -1;

    pisp = &plt_isp->m_isp;

    pisp->ops = &g_isp_ops;

    ht_isp_module_register(pisp);

    return ret;
}


